package leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Lc49 {


    public static List<List<String>> groupAnagrams(String[] strs) {

        HashMap<String,List<String>> map = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            String str = strs[i];
            int[] cnt = new int[26];
            for (char c: str.toCharArray()) {
                cnt[c - 'a'] ++;
            }
            StringBuffer key = new StringBuffer();
            for (int j = 0; j < 26; j++) {
                if (cnt[j] != 0) {
                    key.append('a' + j );
                    key.append(cnt[j]);
                }
            }
            List<String> list = map.getOrDefault(key.toString(),new ArrayList<String>());
            list.add(strs[i]);
            map.put(key.toString(),list);
        }
        return new ArrayList<List<String>>(map.values());
    }
}
